package cn.cheegon.blog.common.api;

import cn.cheegon.blog.common.exception.Asserts;
import cn.hutool.core.util.ReflectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@Data
public class CommonPage<T> {
    /**
     * 当前页码
     */
    private Long pageNum;

    /**
     * 每页数量
     */
    private Long pageSize;

    /**
     * 总页数
     */
    private Long totalPage;

    /**
     * 总条数
     */
    private Long total;

    /**
     * 分页数据
     */
    private List<T> list;

    /**
     * 将PageHelper分页后的list转为分页信息
     *
     * @param pageInfo 原始信息
     * @param dtoList  转化的信息
     * @param <T>
     * @return
     */
    public static <T> CommonPage<T> restPage(Page pageInfo, List<T> dtoList) {
        CommonPage result = new CommonPage();
        result.setTotalPage(pageInfo.getPages());
        result.setPageNum(pageInfo.getPages());
        result.setPageSize(pageInfo.getSize());
        result.setTotal(pageInfo.getTotal());
        result.setList(dtoList);
        return result;
    }

    /**
     * 将SpringData分页后的list转为分页信息
     *
     * @param pageInfo 原始信息
     * @return
     */
    public static CommonPage restPage(Page pageInfo) {
        CommonPage result = new CommonPage();
        result.setTotalPage(pageInfo.getPages());
        result.setPageNum(pageInfo.getPages());
        result.setPageSize(pageInfo.getSize());
        result.setTotal(pageInfo.getTotal());
        result.setList(pageInfo.getRecords());
        return result;
    }

    public static <T> CommonPage<T> restPage(Page pageInfo, Class<T> classz) {
        CommonPage result = new CommonPage();
        result.setTotalPage(pageInfo.getPages());
        result.setPageNum(pageInfo.getPages());
        result.setPageSize(pageInfo.getSize());
        result.setTotal(pageInfo.getTotal());
        List<T> list = new ArrayList<>();
        pageInfo.getRecords().forEach(po -> {
            T dto= ReflectUtil.newInstanceIfPossible(classz);
            BeanUtils.copyProperties(po, dto);
            list.add(dto);
/*            try {
                dto = classz.newInstance();
                BeanUtils.copyProperties(po, dto);
                list.add(dto);
            } catch (InstantiationException e) {
                log.error("restPage fail", e);
                Asserts.fail("rest page fail");
            } catch (IllegalAccessException e) {
                log.error("restPage fail", e);
                Asserts.fail("rest page fail");
            }*/
        });
        result.setList(list);
        return result;
    }

    /**
     * 空数据
     *
     * @param pageInfo
     * @return
     */
    public static CommonPage emptyPage(Page pageInfo) {
        CommonPage result = new CommonPage();
        result.setTotalPage(pageInfo.getPages());
        result.setPageNum(pageInfo.getPages());
        result.setPageSize(pageInfo.getSize());
        result.setTotal(pageInfo.getTotal());
        result.setList(Collections.emptyList());
        return result;
    }
}
